package com.ymx.darling.spring.mapper;

import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

import java.util.Map;

/**
 * @author 爱Java的小于
 */
public class DarlingMapperImportBeanDefinitionRegister implements ImportBeanDefinitionRegistrar {
    /**
     * 注册Bean定义
     *
     * @param importingClassMetadata 导入类的元数据
     * @param beanRegistry Bean定义注册表
     */
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry beanRegistry) {
        // 获取DarlingMapperScanner注解的属性
        Map<String, Object> mapperScannerAttributes = importingClassMetadata.getAnnotationAttributes(DarlingMapperScanner.class.getName());
        // 获取注解中value的值，即Mapper接口所在的包路径
        String mapperPackage = (String) mapperScannerAttributes.get("value");
        // 创建MapperScannerConfigurer对象
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        // 设置Mapper接口的包路径
        mapperScannerConfigurer.setMapperPackage(mapperPackage);
        // 调用postProcessBeanDefinitionRegistry方法处理Bean定义注册表
        mapperScannerConfigurer.postProcessBeanDefinitionRegistry(beanRegistry);
    }
}

